#include "head.h"


int main(int argc, char *argv[]){                                  

    ARGS_CHECK(argc, 3);

    int sockfd = establishConnect(argv[1], argv[2]);

    // 1.先进行登录或者注册
    while(1){
        printf("--------------------------\n");
        printf("enter 1 login\n");
        printf("enter 2 register\n");

        int num = 0;
        scanf("%d", &num);
        if(num == 1){
            // 登录逻辑
            if(loginVerity(sockfd) == 0){
                break;
            }      
        }
        if(num == 2){
            // 注册逻辑
            if(registerVerity(sockfd) == 0){
                break;
            }
        }
        printf("enter error, again\n");
        tcflush(STDIN_FILENO, TCIFLUSH); // 清空终端输入缓冲区
    }

    printf("----instruction---\n");
    char instruction[1024];
    // 2.指令处理
    fd_set fdset;
    while(1){
        FD_ZERO(&fdset);
        FD_SET(sockfd, &fdset);
        FD_SET(STDIN_FILENO, &fdset);

        printf("insturction:>>");
        select(sockfd + 1, &fdset, NULL, NULL, NULL);
        if(FD_ISSET(sockfd, &fdset)){
            printf("you out\n");
            break;
        }

        if(FD_ISSET(STDIN_FILENO, &fdset)){

            bzero(instruction, sizeof(instruction));
            fflush(stdout);
            
            read(STDIN_FILENO, instruction, sizeof(instruction));

            dealInstruction(instruction, sockfd);
        }

        // 清空缓存
        fflush(stdin);
    }
}
